package com.learn.controller;

import com.learn.pojo.User;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.net.URI;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/02/06
 * @description
 */
@RestController
public class MessageController {
//    @RequestMapping("/hello")
//    @ResponseBody
//    public String hello() {
//        return "ok";
//    }
    @RequestMapping("/hello")
    public User hello() {
        return new User(1, "ok", "123");
    }
    @PostMapping("/save")
    public String save(@RequestBody String s) {
        System.out.println(s);
        return s;
    }
    @PostMapping("/save2")
    public User save2(@RequestBody User user) {
        System.out.println(user);
        return user;
    }
    @PostMapping("/request")
    public String request(RequestEntity<User> requestEntity) {
        // 获取请求路径
        URI url = requestEntity.getUrl();
        System.out.println(url);
        // 获取请求方式
        HttpMethod method = requestEntity.getMethod();
        System.out.println(method);
        // 获取请求体的类 class com.learn.pojo.User
        Type type = requestEntity.getType();
        System.out.println(type);
        // 获取请求体，json数据可以自动转换成泛型指定的类,form表单好像不可以
        User body = requestEntity.getBody();
        // 获取完整请求头
        HttpHeaders headers = requestEntity.getHeaders();
        System.out.println(headers);
        System.out.println(body);
        return "ok";
    }
    @GetMapping("/response")
    public ResponseEntity<User> response(@RequestParam("id") Integer id) {
        if(id == 1) {
            User user = new User(1, "abc", "1947");
            return ResponseEntity.ok().body(user);
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
    }
}
